const sqlOptions = require("../../sql");
const {format} = require("../../utils");
const order = require("express").Router()

// 订单列表
order.post("/queryList", async (req, res) => {
    try {
        const {order_status} = req.body
        // 订单表
        const list = await req.db.query(sqlOptions({
            name: "oms_order",
            data: {
                order_status,
            },
            method: "list"
        }))
        // 用户表
        const users = await req.db.query(sqlOptions({
            name: "sms_user",
            data: {},
            method: "list"
        }))
        // 商品表
        const products = await req.db.query(sqlOptions({
            name: "sms_product",
            data: {},
            method: "list"
        }))
        const orders = []

        list.forEach(item => {
            const strs = req.base64.decodeNumber(item.order_no).split("-")
            const product = products.filter(item => item.product_id === strs[0])[0]
            const user = users.filter(item => item.id === Number(strs[1]))[0]
            const obj = {
                product_name: product.product_name,
                product_pic: product.product_pic,
                username: user.username,
                order_product_total: item.order_product_total,
                order_no: item.order_no,
                order_status: item.order_status,
                date: format(new Date(Number(strs[4])), "YYYY-MM-DD HH:mm:ss")
            }
            orders.push(obj)
        })

        return res.status(200).send({
            code: 200,
            message: "",
            data: orders
        })

    } catch (e) {
        return res.status(200).send({
            code: 500,
            message: "系统错误",
            data: ''
        })
    }
})
// 确认发货
order.post("/confirm", async (req, res) => {
    try {
        const {order_no} = req.body
        const no = req.base64.decodeNumber(order_no).split("-")
        // 订单表
        const order = await req.db.query(sqlOptions({
            name: "oms_order",
            data: {
                order_no
            },
            method: "list"
        }))
        // 收货地址表
        const user = await req.db.query(sqlOptions({
            name: "oms_site",
            data: {
                user_id: no[1],
                isDefault: "1"
            },
            if: "AND",
            method: "list"
        }))
        // 省市区表
        const city = await req.db.query(sqlOptions({
            name: "rc_district",
            method: "list",
            data: {
            }
        }))

        const province = city.filter(item => item.district_id === user[0].provinceId)[0]
        const citys = city.filter(item => item.district_id === user[0].cityId)[0]
        const district = city.filter(item => item.district_id === user[0].districtId)[0]
        const obj = {
            order_id: order[0].id,
            address_name: user[0].address_name,
            mobile: user[0].mobile,
            address: `${province.district} ${citys.district} ${district.district} ${user[0].remark}`,
            logistics_status: "0",
            order_no
        }
        // console.log(obj)
        await req.db.query(sqlOptions({
            name: "oms_order_logistics",
            method: "save",
            data: obj
        }))

        await req.db.query(sqlOptions({
            name: "oms_order",
            method: "update",
            data: {
                order_status: "2"
            },
            key: {
                order_no
            }
        }))

        return res.status(200).send({
            code: 200,
            message: '发货成功',
            data: ""
        })
    } catch (e) {
        // console.log(e)
        return res.status(200).send({
            code: 500,
            message: "系统错误",
            data: ''
        })
    }
})

// 退货
order.post("/sales", async (req, res) => {
    try {
        // console.log(req.body)
        const data = req.body
        const strs = req.base64.decodeNumber(req.body.order_no).split("-")
        // 订单状态更新5 退货中
        // console.log(strs)
        await req.db.query(sqlOptions({
            name: "oms_order",
            method: "update",
            data: {
                order_status: "6"
            },
            key: {
                order_no: req.body.order_no
            }
        }))
        const logistics = req.db.query(sqlOptions({
            name: "oms_order_logistics",
            method: "list",
            data: {order_no: req.body.order_no}
        }))
        if (logistics.length) {
            // 物流状态当前id删除
            await req.db.query(sqlOptions({
                name: "oms_order_logistics",
                method: "delete",
                data: {
                    order_no: req.body.order_no
                }
            }))
        }

        // 颜色表, 商品表库存加 订单数量
        //商品id
        // strs[0]
        //颜色id
        // strs[3]
        // 总价格
        // order_product_total
        const products = await req.db.query(sqlOptions({
            name: "sms_product",
            data: {
                product_id: strs[0]
            },
            method: "list"
        }))
        if (products.length) {
            const product = products[0]
            const obj = {
                product_order: 1
            }
            if (product.discount !== 0) {
                obj.count = data.order_product_total / product.discount
            } else {
                obj.count = data.order_product_total / product.product_money
            }
            // 是否更新订单量
            //product_order = product_order - 1,
            await req.db.query(`UPDATE sms_product SET  product_count = product_count + ${obj.count}  WHERE product_id = '${strs[0]}'`)
        }
        // console.log(products)
        return res.status(200).send({
            code: 200,
            message: "退货成功",
            data: ''
        })
    } catch (e) {
        return res.status(200).send({
            code: 500,
            message: "系统错误",
            data: ''
        })
    }
})

module.exports = order
